define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'coupon/index' + location.search,
                    add_url: 'coupon/add',
                    edit_url: 'coupon/edit',
                    del_url: 'coupon/del',
                    multi_url: 'coupon/multi',
                    import_url: 'coupon/import',
                    table: 'coupon',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                searchFormVisible: true,// 默认显示搜索条件
                search: false,// 搜索框
                commonSearch: false,// 搜索按钮
                showToggle: false,// 切换视图
                showColumns: false,// 显示列
                showExport: false,// 导出按钮
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'type', title: __('Type'), searchList: {"1": __('满减优惠卷'), "2": __('维修费用全免卷'), "3": __('专属商品全额抵用卷')}, formatter: Table.api.formatter.label},
                        {field: 'title', title: __('Title'), operate: 'LIKE'},
                        {
                            field:'content',
                            title:__('优惠信息'),
                            //visible:false,
                            formatter:function(value,row,index){
                                var html='';
                                if(row.type == 1){
                                    html+='满'+row.reduction_amount+'元,立减'+row.discount_amount+'元';
                                }
                                if(row.type == 2){
                                    html+='维修费用全免卷';
                                }
                                // if(row.type == 3){
                                //     html+='商品'+row.product_name+'专属全额抵用卷';
                                // }
                                return html;
                            }
                        },
                        {field: 'expiretime', title: __('Expiretime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},

                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $('input:radio[name="row[type]"]').click(function(){
                    var value = $(this).val(); //获取选中的radio的值
                    if(value == 1){
                        $('.reduction').removeClass("hide");
                        $('#c-reduction_amount').val(5);
                        $('#c-discount_amount').val(100);

                        $('.product').addClass("hide");
                        $('#c-product_id').attr("data-rule"," ");
                    }else if(value == 2){
                        $('.reduction').addClass("hide");
                        $('#c-reduction_amount').val(0);
                        $('#c-discount_amount').val(0);

                        $('.product').addClass("hide");
                        $('#c-product_id').attr("data-rule"," ");
                    }else if(value == 3){
                        $('.reduction').addClass("hide");
                        $('#c-reduction_amount').val(0);
                        $('#c-discount_amount').val(0);

                        $('.product').removeClass("hide");
                        $('#c-product_id').attr("data-rule","required");
                    }
                });
                Form.api.bindevent($("form[role=form]"));


            }
        }
    };
    return Controller;
});
